<?php

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;
use QL\QueryList;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Movies extends HomeBase {

    //采集百度云盘电影资源
    public function index() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
        cache('result', Db::name('Movies')->where(['pid' => ['in', [117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]]])->select());

//        for ($i = 1; $i <= 349; $i++) {
//            $list[] = 'http://www.jiesfan.com/baiduyun.html?mcType=0&pageIndex=' . $i;
//        }

        $list[] = 'http://www.jiesfan.com/baiduyun.html?mcType=0&pageIndex=1';

        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {


        //采集规则
        $reg = array(
            //采集文章url
            'type' => array('.tabl2 td em a', 'text'),
            'url' => array('.movie-name', 'href')
        );
        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item) {

            if (isset($item['type']) && $item['type'] == '未知')
                return array();

            $pid = array(
                '动作片' => 117,
                '犯罪片' => 118,
                '喜剧片' => 119,
                '爱情片' => 120,
                '剧情片' => 121,
                '恐怖片' => 122,
                '惊悚片' => 123,
                '科幻片' => 124,
                '战争片' => 125,
                '动画片' => 126,
                '奇幻片' => 127,
                '冒险片' => 128,
                '微电影' => 129,
                '其它' => 130,
            );

            $res = QueryList::Query('http://www.jiesfan.com/' . $item['url'], ['content' => ['.img-intro', 'html'],
                        'title' => ['.movie-content1 h1', 'text', '-span -div'],
                        'cover_image' => ['.movie-faceImg img', 'src'],
                        'cover_introduction' => ['.movie-basic-message', 'html'],
                        'introduction' => ['.p-name-list span', 'text'],
                        'starring' => ['.p-name-list ul', 'html'],
                        'download' => ['.dl-lists-ct', 'html'],
                    ])->data;

            $ress = array();
            $result = cache('result');
            foreach ($res as $k => $v) {
                //如果标题不为空
                if (isset($v['title']) && $v['title'] != '') {
                    if ($result && is_array($result)) {
                        //获取结果集中的标题列转为一维数组
                        $titles = array_column($result, 'title');

                        //如果该视频不存在 那么可以新增
                        if (!in_array($v['title'], $titles)) {
                            $ress['content'] = isset($v['content']) ? $v['content'] : '';
                            $ress['title'] = $v['title'];
                            $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                            $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                            $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                            $ress['download'] = isset($v['download']) ? $v['download'] : '';
                            $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                            $ress['add_time'] = time();
                            $ress['click'] = 0;
                            $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 71;
                        }
                    } else {
                        $ress['content'] = isset($v['content']) ? $v['content'] : '';
                        $ress['title'] = $v['title'];
                        $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                        $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                        $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                        $ress['download'] = isset($v['download']) ? $v['download'] : '';
                        $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                        $ress['add_time'] = time();
                        $ress['click'] = 0;
                        $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 71;
                    }
                }
            }
            return $ress;
        });

        //过滤一遍空数组
        $data = array_filter($data);

        //必须是结果集
        if ($data && is_array($data)) {
            //插入数据库
            Db::name('Movies')->insertAll($data);
        }
        sleep(1); //休息1秒
    }
        ]);
        echo '采集完成！';
    }

    //查询国内的电影
    public function index2() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
        cache('result', Db::name('Movies')->where(['pid' => ['in', [74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88]]])->select());

//        for ($i = 1; $i <= 324; $i++) {
//            $list[] = 'http://www.jiesfan.com/china.html?mcType=0&pageIndex=' . $i;
//        }
        $list[] = 'http://www.jiesfan.com/china.html?mcType=0&pageIndex=1';

        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 2
            ],
            'success' => function($a) {

        //采集规则
        $reg = array(
            //采集文章url
            'type' => array('.tabl2 td em a', 'text'),
            'url' => array('.movie-name', 'href')
        );

        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item) {

            if (isset($item['type']) && $item['type'] == '未知')
                return array();

            $pid = array(
                '动作片' => 74,
                '犯罪片' => 75,
                '喜剧片' => 76,
                '爱情片' => 77,
                '剧情片' => 78,
                '恐怖片' => 79,
                '惊悚片' => 80,
                '科幻片' => 81,
                '战争片' => 82,
                '动画片' => 83,
                '奇幻片' => 84,
                '冒险片' => 85,
                '微电影' => 87,
                '其它' => 88,
            );

            $res = QueryList::Query('http://www.jiesfan.com/' . $item['url'], ['content' => ['.img-intro', 'html'],
                        'title' => ['.movie-content1 h1', 'text', '-span -div'],
                        'cover_image' => ['.movie-faceImg img', 'src'],
                        'cover_introduction' => ['.movie-basic-message', 'html'],
                        'introduction' => ['.p-name-list span', 'text'],
                        'starring' => ['.p-name-list ul', 'html'],
                        'download' => ['.dl-lists-ct', 'html'],
                    ])->data;

            //搜索标题 折中处理BT下载连接问题
            if (isset($res[0]['title'])) {
                $post_res = QueryList::run('Request', [
                            'target' => 'http://www.mxroom.com/search.php',
                            'method' => 'POST',
                            'params' => ['mod' => 'forum', 'formhash' => 'cd779d86', 'srchtxt' => $res[0]['title'], 'searchsubmit' => 'yes'],
                ]);

                if ($post_res->html && !empty($post_res->html)) {
                    $post_data = $post_res->setQuery(['true_download' => ['#threadlist a:first', 'href']])->data;
                    if (isset($post_data[0]['true_download']) && $post_data[0]['true_download']) {
                        $post_data2 = QueryList::Query($post_data[0]['true_download'], ['true_download' => ['ignore_js_op a:first', 'href']])->data;

                        if (isset($post_data2[0]['true_download']) && $post_data2[0]['true_download']) {
                            $post_data3 = QueryList::Query('http://www.mxroom.com/' . $post_data2[0]['true_download'], ['true_download' => ['.dxksst a', 'href'], 'download_size' => ['.dxksst div p:eq(3)', 'text'], 'download_type' => ['.dxksst div p:eq(2)', 'text']])->data;
                            if (isset($post_data3[0]['download_size']) && $post_data3[0]['download_size']) {
                                $post_data3[0]['download_size'] = str_replace(' 附件大小：', '', $post_data3[0]['download_size']);
                                $post_data3[0]['download_type'] = str_replace(' 附件格式：', '', $post_data3[0]['download_type']);
                            }
                        }
                    }
                }
            }

            if (!isset($post_data3[0]['download_size']))
                return array();

            $ress = array();
            $result = cache('result');

            foreach ($res as $k => $v) {
                //如果标题不为空
                if (isset($v['title']) && $v['title'] != '') {
                    if ($result && is_array($result)) {
                        //获取结果集中的标题列转为一维数组
                        $titles = array_column($result, 'title');
                        //如果该视频不存在 那么可以新增
                        if (!in_array($v['title'], $titles)) {
                            $ress['content'] = isset($v['content']) ? $v['content'] : '';
                            $ress['title'] = $v['title'];
                            $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                            $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                            $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                            $ress['download'] = isset($v['download']) ? $v['download'] : '';
                            $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                            $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                            $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                            $ress['download_num'] = 0;
                            $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                            $ress['add_time'] = time();
                            $ress['click'] = 0;
                            $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 68;
                        }
                    } else {
                        $ress['content'] = isset($v['content']) ? $v['content'] : '';
                        $ress['title'] = $v['title'];
                        $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                        $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                        $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                        $ress['download'] = isset($v['download']) ? $v['download'] : '';
                        $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                        $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                        $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                        $ress['download_num'] = 0;
                        $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                        $ress['add_time'] = time();
                        $ress['click'] = 0;
                        $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 68;
                    }
                }
            }
            if ($ress && $ress != '') {
                Db::name('Movies')->insert($ress);
//                        sleep(1); //休息1秒
            }
//                    return $ress;
        });
        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }

    //查询欧美的电影
    public function index3() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
        cache('result', Db::name('Movies')->where(['pid' => ['in', [89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102]]])->select());

        $list[] = 'http://www.jiesfan.com/western.html?mcType=0&pageIndex=1';


        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {

        //采集规则
        $reg = array(
            //采集文章url
            'type' => array('.tabl2 td em a', 'text'),
            'url' => array('.movie-name', 'href')
        );

        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item) {

            if (isset($item['type']) && $item['type'] == '未知')
                return array();

            $pid = array(
                '动作片' => 89,
                '犯罪片' => 90,
                '喜剧片' => 91,
                '爱情片' => 92,
                '剧情片' => 93,
                '恐怖片' => 94,
                '惊悚片' => 95,
                '科幻片' => 96,
                '战争片' => 97,
                '动画片' => 98,
                '奇幻片' => 99,
                '冒险片' => 100,
                '微电影' => 101,
                '其它' => 102,
            );

            $res = QueryList::Query('http://www.jiesfan.com/' . $item['url'], ['content' => ['.img-intro', 'html'],
                        'title' => ['.movie-content1 h1', 'text', '-span -div'],
                        'cover_image' => ['.movie-faceImg img', 'src'],
                        'cover_introduction' => ['.movie-basic-message', 'html'],
                        'introduction' => ['.p-name-list span', 'text'],
                        'starring' => ['.p-name-list ul', 'html'],
                        'download' => ['.dl-lists-ct', 'html'],
                    ])->data;

            //搜索标题 折中处理BT下载连接问题
            if (isset($res[0]['title'])) {
                $post_res = QueryList::run('Request', [
                            'target' => 'http://www.mxroom.com/search.php',
                            'method' => 'POST',
                            'params' => ['mod' => 'forum', 'formhash' => 'cd779d86', 'srchtxt' => $res[0]['title'], 'searchsubmit' => 'yes'],
                ]);

                if ($post_res->html && !empty($post_res->html)) {
                    $post_data = $post_res->setQuery(['true_download' => ['#threadlist a:first', 'href']])->data;
                    if (isset($post_data[0]['true_download']) && $post_data[0]['true_download']) {
                        $post_data2 = QueryList::Query($post_data[0]['true_download'], ['true_download' => ['ignore_js_op a:first', 'href']])->data;

                        if (isset($post_data2[0]['true_download']) && $post_data2[0]['true_download']) {
                            $post_data3 = QueryList::Query('http://www.mxroom.com/' . $post_data2[0]['true_download'], ['true_download' => ['.dxksst a', 'href'], 'download_size' => ['.dxksst div p:eq(3)', 'text'], 'download_type' => ['.dxksst div p:eq(2)', 'text']])->data;
                            if (isset($post_data3[0]['download_size']) && $post_data3[0]['download_size']) {
                                $post_data3[0]['download_size'] = str_replace(' 附件大小：', '', $post_data3[0]['download_size']);
                                $post_data3[0]['download_type'] = str_replace(' 附件格式：', '', $post_data3[0]['download_type']);
                            }
                        }
                    }
                }
            }

            if (!isset($post_data3[0]['download_size']))
                return array();

            $ress = array();
            $result = cache('result');

            foreach ($res as $k => $v) {
                //如果标题不为空
                if (isset($v['title']) && $v['title'] != '') {
                    if ($result && is_array($result)) {
                        //获取结果集中的标题列转为一维数组
                        $titles = array_column($result, 'title');
                        //如果该视频不存在 那么可以新增
                        if (!in_array($v['title'], $titles)) {
                            $ress['content'] = isset($v['content']) ? $v['content'] : '';
                            $ress['title'] = $v['title'];
                            $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                            $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                            $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                            $ress['download'] = isset($v['download']) ? $v['download'] : '';
                            $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                            $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                            $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                            $ress['download_num'] = 0;
                            $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                            $ress['add_time'] = time();
                            $ress['click'] = 0;
                            $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 69;
                        }
                    } else {
                        $ress['content'] = isset($v['content']) ? $v['content'] : '';
                        $ress['title'] = $v['title'];
                        $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                        $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                        $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                        $ress['download'] = isset($v['download']) ? $v['download'] : '';
                        $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                        $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                        $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                        $ress['download_num'] = 0;
                        $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                        $ress['add_time'] = time();
                        $ress['click'] = 0;
                        $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 69;
                    }
                }
            }
            if ($ress && $ress != '') {
                Db::name('Movies')->insert($ress);
            }
        });

        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }

    //查询亚洲电影
    public function index4() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
        cache('result', Db::name('Movies')->where(['pid' => ['in', [103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116]]])->select());

        $list[] = 'http://www.jiesfan.com/asia.html?mcType=0&pageIndex=1';


        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {

        //采集规则
        $reg = array(
            //采集文章url
            'type' => array('.tabl2 td em a', 'text'),
            'url' => array('.movie-name', 'href')
        );

        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item) {

            if (isset($item['type']) && $item['type'] == '未知')
                return array();

            $pid = array(
                '动作片' => 103,
                '犯罪片' => 104,
                '喜剧片' => 105,
                '爱情片' => 106,
                '剧情片' => 107,
                '恐怖片' => 108,
                '惊悚片' => 109,
                '科幻片' => 110,
                '战争片' => 111,
                '动画片' => 112,
                '奇幻片' => 113,
                '冒险片' => 114,
                '微电影' => 115,
                '其它' => 116,
            );

            $res = QueryList::Query('http://www.jiesfan.com/' . $item['url'], ['content' => ['.img-intro', 'html'],
                        'title' => ['.movie-content1 h1', 'text', '-span -div'],
                        'cover_image' => ['.movie-faceImg img', 'src'],
                        'cover_introduction' => ['.movie-basic-message', 'html'],
                        'introduction' => ['.p-name-list span', 'text'],
                        'starring' => ['.p-name-list ul', 'html'],
                        'download' => ['.dl-lists-ct', 'html'],
                    ])->data;

            //搜索标题 折中处理BT下载连接问题
            if (isset($res[0]['title'])) {
                $post_res = QueryList::run('Request', [
                            'target' => 'http://www.mxroom.com/search.php',
                            'method' => 'POST',
                            'params' => ['mod' => 'forum', 'formhash' => 'cd779d86', 'srchtxt' => $res[0]['title'], 'searchsubmit' => 'yes'],
                ]);

                if ($post_res->html && !empty($post_res->html)) {
                    $post_data = $post_res->setQuery(['true_download' => ['#threadlist a:first', 'href']])->data;
                    if (isset($post_data[0]['true_download']) && $post_data[0]['true_download']) {
                        $post_data2 = QueryList::Query($post_data[0]['true_download'], ['true_download' => ['ignore_js_op a:first', 'href']])->data;

                        if (isset($post_data2[0]['true_download']) && $post_data2[0]['true_download']) {
                            $post_data3 = QueryList::Query('http://www.mxroom.com/' . $post_data2[0]['true_download'], ['true_download' => ['.dxksst a', 'href'], 'download_size' => ['.dxksst div p:eq(3)', 'text'], 'download_type' => ['.dxksst div p:eq(2)', 'text']])->data;
                            if (isset($post_data3[0]['download_size']) && $post_data3[0]['download_size']) {
                                $post_data3[0]['download_size'] = str_replace(' 附件大小：', '', $post_data3[0]['download_size']);
                                $post_data3[0]['download_type'] = str_replace(' 附件格式：', '', $post_data3[0]['download_type']);
                            }
                        }
                    }
                }
            }

            if (!isset($post_data3[0]['download_size']))
                return array();

            $ress = array();
            $result = cache('result');

            foreach ($res as $k => $v) {
                //如果标题不为空
                if (isset($v['title']) && $v['title'] != '') {
                    if ($result && is_array($result)) {
                        //获取结果集中的标题列转为一维数组
                        $titles = array_column($result, 'title');
                        //如果该视频不存在 那么可以新增
                        if (!in_array($v['title'], $titles)) {
                            $ress['content'] = isset($v['content']) ? $v['content'] : '';
                            $ress['title'] = $v['title'];
                            $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                            $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                            $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                            $ress['download'] = isset($v['download']) ? $v['download'] : '';
                            $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                            $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                            $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                            $ress['download_num'] = 0;
                            $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                            $ress['add_time'] = time();
                            $ress['click'] = 0;
                            $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 70;
                        }
                    } else {
                        $ress['content'] = isset($v['content']) ? $v['content'] : '';
                        $ress['title'] = $v['title'];
                        $ress['cover_image'] = isset($v['cover_image']) ? $v['cover_image'] : '';
                        $ress['cover_introduction'] = isset($v['cover_introduction']) ? $v['cover_introduction'] : '';
                        $ress['starring'] = isset($v['starring']) ? $v['starring'] : '';
                        $ress['download'] = isset($v['download']) ? $v['download'] : '';
                        $ress['true_download'] = isset($post_data3[0]['true_download']) ? $post_data3[0]['true_download'] : '';
                        $ress['download_size'] = isset($post_data3[0]['download_size']) ? $post_data3[0]['download_size'] : '';
                        $ress['download_type'] = isset($post_data3[0]['download_type']) ? $post_data3[0]['download_type'] : '';
                        $ress['download_num'] = 0;
                        $ress['introduction'] = isset($v['introduction']) ? trim($v['introduction'], '  ') : '';
                        $ress['add_time'] = time();
                        $ress['click'] = 0;
                        $ress['pid'] = isset($item['type']) ? $pid[$item['type']] : 70;
                    }
                }
            }
            if ($ress && $ress != '') {
                Db::name('Movies')->insert($ress);
            }
        });

        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }

    /**
     * 更新字段
     */
    public function updateFile() {
        //update zhuzong_movies set introduction='' where introduction='  ';
        Db::name('Movies')->where(['introduction' => '  '])->update(['introduction' => '']);
    }
    
    //采集文章
    public function index5() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
        cache('result', Db::name('article')->select());

//        for ($i = 1; $i <= 340; $i++) {
//            $list[] = 'http://www.38dy.cn/a/tv-news/list_' . $i.'.html';
//        }

        $list[] = 'http://www.38dy.cn/a/tv-news/list_1.html';

        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {


        //采集规则
        $reg = array(
            //采集文章url
            'url' => array('#list tr td h2 a', 'href'),
            'introduction' => array('#list tr td p:eq(0)', 'text')
        );
        $ql = QueryList::Query($a['content'], $reg,'','UTF-8','GBK');

        $data = $ql->getData(function($item) {
            
            if(isset($item['url']) && isset($item['introduction'])){
                $res = QueryList::Query('http://www.38dy.cn' . $item['url'], ['content' => ['#main-container .post-content', 'html','-script'],
                            'title' => ['#main-container h1', 'text'],
                        ])->data;

                $ress = array();
                $result = cache('result');
                foreach ($res as $k => $v) {
                    //如果标题不为空
                    if (isset($v['title']) && $v['title'] != '') {
                        if ($result && is_array($result)) {
                            //获取结果集中的标题列转为一维数组
                            $titles = array_column($result, 'title');

                            //如果该视频不存在 那么可以新增
                            if (!in_array($v['title'], $titles)) {
                                $ress['content'] = isset($v['content']) ? str_replace('内容由38电影网提供：', '', $v['content']) : '';
                                $ress['introduction'] = $item['introduction'];
                                $ress['title'] = $v['title'];
                                $ress['reading'] = 0;
                                $ress['publish_time'] = time();
                            }
                        } else {
                            $ress['content'] = isset($v['content']) ? str_replace('内容由38电影网提供：', '', $v['content']) : '';
                            $ress['introduction'] = $item['introduction'];
                            $ress['title'] = $v['title'];
                            $ress['reading'] = 0;
                            $ress['publish_time'] = time();
                        }
                    }
                }
                return $ress;
                }
            });
        

        //过滤一遍空数组
        $data = array_filter($data);

        //必须是结果集
        if ($data && is_array($data)) {
            //插入数据库
            Db::name('article')->insertAll($data);
        }
        sleep(1); //休息1秒
    }
        ]);
        echo '采集完成！';
    }
    
    
    
//    public function index6 () {
//        
//        // 需要填写你的 Access Key 和 Secret Key
//        $accessKey = 'K2o75dNBZsaEWs-7A6xZ1JNaX0XdSNjIvkDCwcnH';
//        $secretKey = 'ZpLkdGh8v_a4cPWTkglI1s3QYDVWE_0M-PFiA2Mu';
//
//        // 构建鉴权对象
//        $auth = new Auth($accessKey, $secretKey);
//
//        // 要上传的空间
//        $bucket = 'zhuzong';
//
//        // 生成上传 Token
//        $token = $auth->uploadToken($bucket);
//
//        // 要上传文件的本地路径
//        $filePath = './static/images/favicon.png';
//
//        // 上传到七牛后保存的文件名
//        $key = time().'.png';
//
//        // 初始化 UploadManager 对象并进行文件的上传。
//        $uploadMgr = new UploadManager();
//
//        // 调用 UploadManager 的 putFile 方法进行文件的上传。
//        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
//        echo "\n====> putFile result: \n";
//        if ($err !== null) {
//           var_dump($err);
//        } else {
//           var_dump($ret);
//        }
//    }
    
    
    
//    public function task() {
//        set_time_limit(0);
//        ini_set('memory_limit', '2048M'); // 设置内存
//        
//        $list[] = 'http://www.ziyuanjuhe.cn/';
//        
//        for($i=1;$i<100000;$i++){
//            $headers = $this->randIp();
////            p($headers);
//            QueryList::run('Multi', [
//                //待采集链接集合
//                'list' => $list,
//                'curl' => [
//                    'opt' => array(
//                        //这里根据自身需求设置curl参数
//                        CURLOPT_SSL_VERIFYPEER => false,
//                        CURLOPT_SSL_VERIFYHOST => false,
//                        CURLOPT_FOLLOWLOCATION => true,
//                        CURLOPT_AUTOREFERER => true,
//                        CURLOPT_HTTPHEADER =>$headers,
//                    //........
//                    ),
//                    //设置线程数
//                    'maxThread' => 1000,
//                    //设置最大尝试数
//                    'maxTry' => 1000
//                ],
//                'success' => function($a) {
//                echo 1;
//                    $ql = QueryList::run('Request',[
//                        'http' => [
//                            'target' => 'http://www.ziyuanjuhe.cn/',
//                            'referrer' => 'http://www.ziyuanjuhe.cn/',
//                            'method' => 'GET',
//                        ],
//                        'callback' => function($html,$args){
//                            echo 2;
//                        },
//                    ]);
//                }
//            ]);
//        }
//        echo '完成！';
//    }
//       
//    //此函数提供了国内的IP地址
    public function randIP(){
       $ip_long = array(
           array('607649792', '608174079'), //36.56.0.0-36.63.255.255
           array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
           array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
           array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
           array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
           array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
           array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
           array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
           array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
           array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
       );
       $rand_key = mt_rand(0, 9);
       $ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
       $headers['CLIENT-IP'] = $ip; 
       $headers['X-FORWARDED-FOR'] = $ip; 

       $headerArr = array(); 
       foreach( $headers as $n => $v ) { 
           $headerArr[] = $n .':' . $v;  
       }
       return $headerArr;    
   }
    
    //110找律师  http://lawyer.110.com
    public function index8() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
//        cache('result', Db::name('Ls')->where(['pid' => ['in', [103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116]]])->select());

        for ($i = 1; $i <= 31; $i++) {
            $list[] = 'http://lawyer.110.com/lawyer/region/rid/' . $i;
        }


        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 100,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {

            $url = explode('/', $a['info']['url']);  //转化url为数组
                
            $pid = $url[count($url)-1];  //获取当前的pid
            
        //采集规则
        $reg = array(
            //获取总数量是多少
            'num' => array('.page-totlerecord', 'text'),
        );

        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item) use ($pid){
            
            if($item['num']){
                $page = ceil($item['num']/10);  //计算出多少页
                
                //获取循环列表的数据
                for ($i = 1; $i <= $page; $i++) {
                    $post_res = QueryList::run('Request', [
                            'target' => 'http://lawyer.110.com/lawyer/region/rid/'.$pid.'/p/'.$i,
                            'method' => 'GET',
                    ]);
                    $post_data = $post_res->setQuery(array('addr'=>['.s01', 'text','-span -font'],'name'=>['.s01 a', 'text'],'phone'=>['.tell', 'text']))->data;
                    
                    if($post_data && is_array($post_data)){
                        Db::name('Ls2')->insertAll($post_data);
                    }
                }
            }
        });

        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }
    
    //华律网  http://www.66law.cn/
    public function index9() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存

        $list = [
            'http://www.66law.cn/anhui/lawyer/page_2.aspx',
            'http://www.66law.cn/fujian/lawyer/page_2.aspx',
            'http://www.66law.cn/jiangsu/lawyer/page_2.aspx',
            'http://www.66law.cn/jiangxi/lawyer/page_2.aspx',
            'http://www.66law.cn/shandong/lawyer/page_2.aspx',
            'http://www.66law.cn/shanghai/lawyer/page_2.aspx',
            'http://www.66law.cn/zhejiang/lawyer/page_2.aspx',
            'http://www.66law.cn/aomen/lawyer/page_2.aspx',
            'http://www.66law.cn/guangdong/lawyer/page_2.aspx',
            'http://www.66law.cn/guangxi/lawyer/page_2.aspx',
            'http://www.66law.cn/hainan/lawyer/page_2.aspx',
            'http://www.66law.cn/taiwan/lawyer/page_2.aspx',
            'http://www.66law.cn/xianggang/lawyer/page_2.aspx',
            'http://www.66law.cn/henan/lawyer/page_2.aspx',
            'http://www.66law.cn/hubei/lawyer/page_2.aspx',
            'http://www.66law.cn/hunan/lawyer/page_2.aspx',
            'http://www.66law.cn/beijing/lawyer/page_2.aspx',
            'http://www.66law.cn/hebei/lawyer/page_2.aspx',
            'http://www.66law.cn/neimenggu/lawyer/page_2.aspx',
            'http://www.66law.cn/shanxi/lawyer/page_2.aspx',
            'http://www.66law.cn/tianjin/lawyer/page_2.aspx',
            'http://www.66law.cn/gansu/lawyer/page_2.aspx',
            'http://www.66law.cn/ningxia/lawyer/page_2.aspx',
            'http://www.66law.cn/qinghai/lawyer/page_2.aspx',
            'http://www.66law.cn/shanxi1/lawyer/page_2.aspx',
            'http://www.66law.cn/xinjiang/lawyer/page_2.aspx',
            'http://www.66law.cn/chongqing/lawyer/page_2.aspx',
            'http://www.66law.cn/guizhou/lawyer/page_2.aspx',
            'http://www.66law.cn/sichuan/lawyer/page_2.aspx',
            'http://www.66law.cn/xicang/lawyer/page_2.aspx',
            'http://www.66law.cn/yunnan/lawyer/page_2.aspx',
            'http://www.66law.cn/heilongjiang/lawyer/page_2.aspx',
            'http://www.66law.cn/jilin/lawyer/page_2.aspx',
            'http://www.66law.cn/liaoning/lawyer/page_2.aspx',
        ];
        
        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                    CURLOPT_HTTPHEADER => $this->randIP(),
                //........
                ),
                //设置线程数
                'maxThread' => 1,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {

            $url = explode('/', $a['info']['url']);  //转化url为数组
                
            $pid = $url[3];  //获取当前的城市

             //采集规则
            $reg = array(
                //采集文章url
                'page' => array('#lawyeronlinepage a', 'text'),
            );
            $ql = QueryList::Query($a['content'], $reg);

            //折中获取分页的长度
            $count = isset($ql->data) ? count($ql->data) : '';

            $page_num = '';
            
            if($count >= 4 ){  //说明一共有2页  < 1 2 >
                $page_num = $ql->data[$count-2];
            }
            
            if($page_num){ //说明可以采集
                //获取循环列表的数据
                for ($i=2; $i <=$page_num['page']; $i++) {
                    $post_res = QueryList::run('Request', [
                            'target' => 'http://www.66law.cn/'.$pid.'/lawyer/page_'.$i.'.aspx',
                            'method' => 'GET',
                    ]);
                    $post_data = $post_res->setQuery(array('name'=>['.find-list5 .f18', 'text'],'phone'=>['.find-list5 .showphone', 'text'],'addr'=>['.find-list5 p.s-c666', 'text','-span -font']))->data;
                    
                    if($post_data && is_array($post_data)){
                        Db::name('Ls')->insertAll($post_data);
                    }
                }
            }
            
        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }
    
    
    
    //会计  http://shanghai.liebiao.com/kuaijishenji/
    public function index10() {

        //永不超时    
        set_time_limit(0);
        ini_set('memory_limit', '2048M'); // 设置内存
        //把结果先查出来放在缓存中
//        cache('result', Db::name('Ls')->where(['pid' => ['in', [103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116]]])->select());

        for ($i = 1; $i <= 20; $i++) {
            $list[] = 'http://shanghai.liebiao.com/kuaijishenji/index' . $i.'.html';
        }


        //多线程扩展
        QueryList::run('Multi', [
            //待采集链接集合
            'list' => $list,
            'curl' => [
                'opt' => array(
                    //这里根据自身需求设置curl参数
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                //........
                ),
                //设置线程数
                'maxThread' => 100,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a) {

        //采集规则
        $reg = array(
            //获取总数量是多少
            'phone' => array('.info-list .click_btn', 'data-phone'),
            'name' => array('.info-list .address', 'text'),
        );

        $ql = QueryList::Query($a['content'], $reg);

        $data = $ql->getData(function($item){
            $post_data = [
                'phone'=>$item['phone'],
                'name'=>isset($item['name']) ? $item['name'] : ''
            ];
            Db::name('Ls3')->insert($post_data);
        });

        //过滤一遍空数组
//                $data = array_filter($data);
        //必须是结果集
//                if($data && is_array($data)){
        //插入数据库
//                    Db::name('Movies3')->insertAll($data);
//                }
//                sleep(1); //休息1秒
    }, 'error' => function($a) {
        echo $a['error'][1];
    }
        ]);
        echo '采集完成！';
    }
    
    

}
